//app.js
var server = require('utils/server');
var common = require('utils/common');
App({
  onLaunch: function(options) {
    this.globalData.accountRandom='';
    this.globalData.zgAccountRandom='';
    console.log('onLaunch')
    wx.removeStorageSync('storeId');
    this.globalData.isRemarked=false;
  },
  onShow: function(options) {
    this.globalData.sceneId = options.scene;
    this.globalData.formSceneId = options.scene;
    wx.getSystemInfo({
      success: res => {
        if (res.model == 'iPhone X') {
          console.log('iPhone X')
        }
        console.log((res.SDKVersion))
        var sdkversion = res.SDKVersion;
        this.globalData.sdkversion = parseInt(sdkversion.replace(/\./g, "")); //基础库版本
        var version = res.version;
        this.globalData.wxversion = parseInt(version.replace(/\./g, ""));
        console.log(this.globalData.wxversion)
        if (parseInt(sdkversion.replace(/\./g, "")) > 199) {
          const updateManager = wx.getUpdateManager();
          updateManager.onCheckForUpdate(function(resp) {
            // 请求完新版本信息的回调
          })
          updateManager.onUpdateReady(function() {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function(resps) {
                if (resps.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function() {
            // 新的版本下载失败
            wx.showModal({
              title: '更新提示',
              content: '新版本下载失败',
              showCancel: false
            })
          })
        }
      }
    })
    console.log('onshow')
    console.log(this.globalData.sceneId)
    console.log(this.globalData.formSceneId)
  },
  actionReport: (() => {
    let actionReport
    try {
      actionReport = typeof requirePlugin === 'function' ? requirePlugin('WXPayMpActionReporter') : null
    } catch (e) {
      console.error('初始化WXPayMpActionReporter失败', e)
    }
    return actionReport || (() => {})
  })(),
  onHide: function() {
    console.log('App Hide')
  },
  bezier: function(pots, amount) {
    var pot;
    var lines;
    var ret = [];
    var points;
    for (var i = 0; i <= amount; i++) {
      points = pots.slice(0);
      lines = [];
      while (pot = points.shift()) {
        if (points.length) {
          lines.push(pointLine([pot, points[0]], i / amount));
        } else if (lines.length > 1) {
          points = lines;
          lines = [];
        } else {
          break;
        }
      }
      ret.push(lines[0]);
    }

    function pointLine(points, rate) {
      var pointA, pointB, pointDistance, xDistance, yDistance, tan, radian, tmpPointDistance;
      var ret = [];
      pointA = points[0]; //点击
      pointB = points[1]; //中间
      xDistance = pointB.x - pointA.x;
      yDistance = pointB.y - pointA.y;
      pointDistance = Math.pow(Math.pow(xDistance, 2) + Math.pow(yDistance, 2), 1 / 2);
      tan = yDistance / xDistance;
      radian = Math.atan(tan);
      tmpPointDistance = pointDistance * rate;
      ret = {
        x: pointA.x + tmpPointDistance * Math.cos(radian),
        y: pointA.y + tmpPointDistance * Math.sin(radian)
      };
      return ret;
    }
    return {
      'bezier_points': ret
    };
  },
  globalData: {
    openid: null,
    latitude: null,//根据地址变化的经纬度
    longitude: null,//根据地址变化的经纬度
    sdkversion: "0",
    wxversion: "0",
    sso: null,
    addrId: null,
    skipType: 0, //默认重新定位，1:其他页面选择模糊地址2：选择门店或者收货地址
    isLocate: true,
    location: null,
    addrType: 0,//1:从到家业务结算页进入;2:修改地址返回生鲜到家页;3:从购物车进;4:修改地址返回结算页;
    refreshTag: true,//返回是否刷新商品列表页
    deliverType: null,
    scene: null,
    sceneId: null,
    isLogin: true,
    isDeleteByStore: false,
    isAccount: false,
    formSceneId: '',
    showRedPackets: true,
    companyid: "",
    storeType: "",
    city: "",
    locateLongitude: "", //当前定位经纬度
    locateLatitude: "", //当前定位经纬度
    locateCity: "", //当前定位城市
    locateProvince: "", //当前定位省份
    isMarked: 0, //落地页是否首次进入(0：首次，1：从其他页面返回)
    isGetCartByAdd: false,//购物车是否通过地址获取
    isBindYcCard: false,
    isFromHome: false, //从首页广告位进入详情页
    isFromCart: false, //从购物车返回详情页
    isFromEditAddr: false, //从购物车返回结算页
    isGetWxUserinfo: false,
    isEditToAccount: false, //从地址页回会员专供结算页
    hyzgAddrId: '',//会员专供地址id
    editAddrCity: "", //地址编辑选择所在的城市
    editAddrCityId: "", //地址编辑选择所在的城市
    editAddrLatitude: "", //地址编辑选择详细地址经度
    editAddrLongitude: "", //地址编辑选择详细地址纬度
    editAddrLocation: "", //地址编辑选择详细地址
    editAddrPname: "", //地址编辑选择省
    editAddrCityname: "", //地址编辑选择城市
    editAddrAdname: "", //地址编辑选择区
    searchAddrCity: "", //搜索地址选择的城市
    selectTempCity: "", //选择地址选择的城市
    selectTempCityId: "", //选择地址选择的城市Id
    isChangeIndexLocation: false, //是否从首页定位入口修改定位
    orderType:0,//订单类型：0到家，1会员专供
    searchOrderStatus:0,//订单中心初始化状态值
    cartIndexType:0,//1 专供，2到家
    isRemarked:false,
    isRefreshAccountList:false, //是否绑定新卡后刷新结算页
    accountRandom:"",//到家结算校验随机数
    zgAccountRandom:""//到家结算校验随机数
  }
})